﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace eLearning.Pages.PanelUtilities
{
    public class ButtonSize : ButtonEvent
    {
        private double ParentHeight;
        private double ParentWidth;
        private Panel SuperParent;
        private bool IsTrueSize;

        private Thickness ParentMargin;
        private static readonly Thickness Zero = new Thickness(0, 0, 0, 0);

        private HorizontalAlignment ParentHorizontalAlignment;
        private VerticalAlignment ParentVerticalAlignment;

        public ButtonSize() : base()
        {
            base.MouseLeftButtonDown += new MouseButtonEventHandler(ButtonEvent_Click);            
        }

        protected override void ButtonEvent_Loaded(object sender, RoutedEventArgs e)
        {
            base.ButtonEvent_Loaded(sender, e);
            if (ParentPanel == null) return;
            SuperParent = ButtonEvent.FindParent<Panel>(this.ParentPanel);
            this.IsTrueSize = true;

            if (SuperParent == null) return;
            this.SuperParent.SizeChanged += new SizeChangedEventHandler(ParentPanel_SizeChanged);
        }

        private void SaveParentInfo()
        {
            this.ParentHeight = this.ParentPanel.Height;
            this.ParentWidth = this.ParentPanel.Width;
            this.ParentMargin = this.ParentPanel.Margin;
            this.ParentVerticalAlignment = this.ParentPanel.VerticalAlignment;
            this.ParentHorizontalAlignment = this.ParentPanel.HorizontalAlignment;
        }        

        private void SetParentInfo()
        {
            ParentPanel.Margin = Zero;
            ParentPanel.Height = SuperParent.ActualHeight;
            ParentPanel.Width = SuperParent.ActualWidth;
            this.ParentPanel.VerticalAlignment = VerticalAlignment.Top;
            this.ParentPanel.HorizontalAlignment = HorizontalAlignment.Left;
        }

        private void LoadParentInfo()
        {
            this.ParentPanel.Height = this.ParentHeight;
            this.ParentPanel.Width = this.ParentWidth;
            this.ParentPanel.Margin = this.ParentMargin;
            this.ParentPanel.VerticalAlignment = ParentVerticalAlignment;
            this.ParentPanel.HorizontalAlignment = ParentHorizontalAlignment;
        }

        void ParentPanel_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (SuperParent == null) return;
            if (! IsTrueSize)
            {
                ParentPanel.Height = SuperParent.ActualHeight;
                ParentPanel.Width = SuperParent.ActualWidth;
            }
        }              

        private void SetParentSize()
        {
            if (SuperParent == null) return;
            if (IsTrueSize) {this.SaveParentInfo(); this.SetParentInfo();}
            else this.LoadParentInfo();
            IsTrueSize = !IsTrueSize;
        }

        protected override void ButtonEvent_Click(object sender, RoutedEventArgs e)
        {
            base.ButtonEvent_Click(sender, e);
            this.SetParentSize();
        }
    }
}
